/*
 * @Description: 
 * @version: 
 * @Author: wyx
 * @Date: 2020-09-22 15:22:58
 * @LastEditDate: Do not exit
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import ShareData from '@/utils/localStorage.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/layout/Home.vue'),
    children:[
      {
        path:'/home',
        name:"HomeV",
        component:()=>import(/* webpackChunkName: "about" */ '../views/HomeV.vue')
      },
      {
        path:'/user',
        name:"User",
        component:()=>import(/* webpackChunkName: "about" */ '../views/user/User.vue')
      },
      {
        path:'/order',
        name:"Order",
        component:()=>import(/* webpackChunkName: "about" */ '../views/order/Order.vue')
      },
      {
        path: '/member',
        name: 'Member',
        component: () => import(/* webpackChunkName: "about" */ '../views/member/Member.vue'),
      },
      {
        path: '/goods',
        name: 'Goods',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods/Goods.vue'),
      },
      {
        path: '/category',
        name: 'Category',
        component: () => import(/* webpackChunkName: "about" */ '../views/goods/Category.vue'),
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
router.beforeEach((to,from,next)=>{
  // console.log(to);
  // console.log(from);
  if(to.path=="/login"){
    next();
  }else{
    if(ShareData.getToken()!=null){
      next();
    }else{
      router.push("/login");
    }
  }
})

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => {
    console.log("路由添加重复")
  })
}

export default router
